package reg.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;


public class JDBCConfiguracionDAO implements IConfiguracionDAO {
	
	private Connection conn;
	
	public JDBCConfiguracionDAO()
	{
		conn = ConnectionManager.getInstance().checkOut();
	}

	@Override
	public String getConfiguracion(String nombre, String tematica) {
        String sql = "SELECT * FROM Configuracion WHERE (idConfiguracion = ? AND tema = ?)";
        PreparedStatement stmt = null;
        ResultSet result = null;
        
        String a = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, nombre);
            stmt.setString(2, tematica);

            result = stmt.executeQuery();

            result.next();
            
            a = result.getString("configuracion");
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

        return a;
	}

	@Override
	public void insertConfiguracion(String nombre, String tematica, String configuracion) {
		 PreparedStatement stmt = null;
	        String sql = "INSERT INTO Configuracion (idConfiguracion, tema, configuracion) VALUES (?, ?, ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);

	            stmt.setString(1, nombre);
	            stmt.setString(2, tematica);
	            stmt.setString(3, configuracion);

	            stmt.executeUpdate();

	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }

	}

	@Override
	public void deleteConfiguracion(String nombre, String tematica) {
		String sql = "DELETE FROM Configuracion WHERE (idConfiguracion = ? AND tema = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, nombre);
            stmt.setString(2, tematica);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
	}

	@Override
	public List<String> getTodasConfiguracionesPorTematica(String tematica) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        List<String> configRes = new LinkedList<String>();
        String sql = "SELECT * FROM Configuracion WHERE tema = ?";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, tematica);
            result = stmt.executeQuery();
            

            while (result.next()) {
                configRes.add(result.getString("idConfiguracion"));
            }
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
            
        } finally {
            try {
                if (result != null) {
                    result.close();
                }

                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

        return configRes;
	}
}